﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace MapWindowDocking
{
    /// <summary>
    /// A xml writer for the favourites file.
    /// </summary>
    public class FavouriteXmlWriter
    {
        public void CreateNewFavouriteFile(string path)
        {
            XDocument xdoc = new XDocument();
            XComment comment1 = new XComment("This file is created and used by the WindowInfo dockable tools by Nathan Woodrow.");
            XComment comment2 = new XComment("The views format in this file is compatable with the NamedViews tool made by Mapinfo");
            XComment comment3 = new XComment("To use the views from NamedViews copy the each whole view element into this this file.");

            xdoc.Add(comment1);
            xdoc.Add(comment2);
            xdoc.Add(comment3);

            xdoc.Add(new XElement("Favourites"));
            

            xdoc.Save(path);
        }

        public void AddNewFavouriteToFile(Favourite fav,string path)
        {                      
            if (!File.Exists(path))
            { 
                this.CreateNewFavouriteFile(path);
            }

            XDocument xmlDoc = XDocument.Load(path);

            XElement viewElement = new XElement("View", fav.Name);
            viewElement.Add(new XAttribute("X", fav.X));
            viewElement.Add(new XAttribute("Y", fav.Y));
            viewElement.Add(new XAttribute("Zoom", fav.Zoom));
            viewElement.Add(new XAttribute("Unit", fav.Units));
            viewElement.Add(new XAttribute("CoordSys", fav.Coordsys));

            xmlDoc.Element("Favourites").Add(viewElement);

            xmlDoc.Save(path);
        }

    }
}
